package com.music.lib.kugou;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 清洗爬虫酷狗的歌词数据
 */
public class KuGouLrcClear {

    public static void main(String[] args) throws Exception{

        FileInputStream fis = new FileInputStream("test.lr");
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
        BufferedReader br = new BufferedReader(isr);
        String str;
        StringBuilder builder = new StringBuilder();
        while((str=br.readLine())!=null){
            builder.append(str);
        }
        br.close();
        isr.close();
        fis.close();

        String strs[] = builder.toString().split("\\\\r\\\\n");
        builder = new StringBuilder();
        for(String s:strs){
            builder.append(unicode2String(s)).append("\n");
        }


        FileOutputStream fos = new FileOutputStream("test.lr");
        OutputStreamWriter writer = new OutputStreamWriter(fos);
        BufferedWriter wr = new BufferedWriter(writer);
        wr.write(builder.toString());
        wr.flush();
        writer.flush();
        fos.flush();
        fos.close();
        writer.close();
        wr.close();

    }

    static String clearLrc(String lrc){
        String strs[] = lrc.split("\\\\r\\\\n");
        StringBuilder builder = new StringBuilder();
        for(String s:strs){
            builder.append(unicode2String(s)).append("\n");
        }
        return builder.toString();
    }

    private static final Pattern REG_UNICODE = Pattern.compile("[0-9A-Fa-f]{4}");
    private static final Pattern EN_CODE = Pattern.compile("[A-Za-z]{4}");

    static String unicode2String(String str) {
        StringBuilder sb = new StringBuilder();
        int len = str.length();
        for (int i = 0; i < len; i++) {
            char c1 = str.charAt(i);
            if (c1 == '\\' && i < len - 1) {
                char c2 = str.charAt(++i);
                if (c2 == 'u' && i <= len - 5) {
                    String tmp = str.substring(i + 1, i + 5);
                    Matcher matcher = REG_UNICODE.matcher(tmp);
                    if (matcher.find()) {
                        sb.append((char) Integer.parseInt(tmp, 16));
                        i = i + 4;
                    } else {
                        sb.append(c1).append(c2);
                    }
                } else {
                    sb.append(c1).append(c2);
                }
            } else {
                sb.append(c1);
            }
        }
        return sb.toString();
    }

    /**
     * Convert the whole String object.
     * @param str
     * @return
     */
    static String string2Unicode(String str)
    {
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<str.length();i++)
        {
            String tmpStr = Integer.toHexString(str.charAt(i));
            if(tmpStr.length() < 4){
                sb.append("\\u00");
            }else{
                sb.append("\\u");
            }
            sb.append(tmpStr);
        }
        return sb.toString();
    }
    /**
     * Just convert Chinese String
     * @param str
     * @return
     */
    static String chinese2Unicode(String str){
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<str.length();i++)
        {
            String tmpStr = Integer.toHexString(str.charAt(i));

            if(tmpStr.length() < 4){
                sb.append(str.charAt(i));
            }else{
                sb.append("\\u");
                sb.append(tmpStr);
            }
        }
        return sb.toString();
    }

}
